package com.zls.controller;

import com.zls.dto.ContractorDto;
import com.zls.page.PageResult;
import com.zls.page.QueryPageBean;
import com.zls.page.Result;
import com.zls.pojo.Contractor;
import com.zls.pojo.Developer;
import com.zls.service.ContractorService;
import com.zls.service.DeveloperService;
import com.zls.vo.ConProVo;
import com.zls.vo.DevProVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/contractor")
@RestController
public class ContractorController {
    @Autowired
    private ContractorService contractorService;

    @GetMapping("findAll")
    public List<Contractor> findAll(){
        List<Contractor> all = contractorService.findAll();
        return all;
    }

    /**
     * 分页查询
     */
    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
        PageResult page = contractorService.findPage(queryPageBean);
        return page;
    }

    /**
     * 承建商添加
     */
    @PostMapping("add")
    public Result add(@RequestBody ConProVo conProVo){
        int i = contractorService.addAndBind(conProVo.getCname(),conProVo.getPids());
        if (i>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    /**
     * 开发商删除
     */
    @GetMapping("delete")
    public Result delete(@RequestParam("cid")String cid){
        int i = contractorService.deleteCon(cid);
        Result result=new Result();
        if (i>0){
            result.setMessage("删除成功");
            result.setFlag(true);
        }else {
            result.setMessage("删除失败,该承建商还有绑定的工程");
        }
        return result;
    }

    /**
     * 查询修改承建商
     */
    @GetMapping("findByCid")
    public Result findByCid(@RequestParam("cid") String cid){
        ContractorDto contractorDto = contractorService.findByCid(cid);
        Result result=new Result();
        if (contractorDto!=null){
            result.setData(contractorDto);
            result.setFlag(true);
            result.setMessage("success");
        }else {
            result.setMessage("系统错误");
        }
        return result;
    }
    /**
     * 修改承建商
     */
    @PostMapping("edit")
    public Result edit(@RequestBody ConProVo conProVo){
        int i = contractorService.editCon(conProVo);
        Result result=new Result();
        if (i>0){
            result.setMessage("修改成功");
            result.setFlag(true);
        }else {
            result.setMessage("修改失败");
        }
        return result;
    }
}
